<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of notesControllerClass
 *
 * @author nstevens
 */
include_once 'commonController.php';

class notesControllerClass extends commonController {

    public $model;

    public function __construct($model) {
        $this->model = $model;
    }

    public function processExistingNotes() {
        //print_r($_POST);
        if (($index = getIndexClickedGenericList("button", 20)) != -1) {
            if (strcmp("Mark as complete", $_POST["button$index"]) === 0) {
                $this->processNotesMarkAsComplete($index);
            } else if (strcmp("Update Notes", $_POST["button$index"]) === 0) {
                $this->processEditNotes($index);
            } else if (strcmp("Save Grade", $_POST["button$index"]) === 0 || strcmp("Save All", $_POST["button$index"])) {
                //if (isset($_POST["button$index"]) || isset($_POST['buttonAll'])) {
                    //echo 'Yup ';
                    if (!$this->processSaveGrade($index)) {
                        return NULL;
                    }
                //}
                $url = $_SERVER['HTTP_REFERER'];
                header("location:$url&update=Grade updated Successfully&activeDiv=grading");
            }
        } else if (isset($_POST['buttonAll'])) {
            for ($i = 0; $i < sizeof(getGradingCategoryArray()); $i++) {
                if (!$this->processSaveGrade($i)) {
                    return NULL;
                }
            }
            $url = $_SERVER['HTTP_REFERER'];
            header("location:$url&update=Grade(s) updated Successfully&activeDiv=grading");
        }
    }

    protected function processSaveGrade($i) {
        $category = $_POST['category' . $i . ''];
        $score = $_POST['scoreDropdown' . $i . ''];
        $comments = $_POST['commentsTextField' . $i . ''];
        $wbsID = $_POST['wbsID'];
        //echo $wbsID;
        $personID = $_POST['personID'];
        //see if there is an entry for this project and category
        $projectResult = executeSelect("SELECT * FROM notes WHERE wbsID='$wbsID' AND subtype='$category'and personId=$personID");
        if ($projectResult && $projectResult->num_rows > 0) {
            //update
            //echo "UPDATE notes set value='$score', notes='$comments' WHERE wbsID='$wbsID' AND subtype='$category' AND personID=$personID";
            $updateResult = executeUpdate("UPDATE notes set value='$score', notes='$comments' WHERE wbsID='$wbsID' AND subtype='$category' AND personID=$personID");
        } else {
            //insert
//            echo "INSERT into notes (value, notes, wbsID, type, subType, personID) 
//                                                          values ('$score','$comments','$wbsID', 'grading', '$category', '$personID')";
            $updateResult = executeUpdate("INSERT into notes (value, notes, wbsID, type, subType, personID) 
                                                          values ('$score','$comments','$wbsID', 'grading', '$category', '$personID')");
        }
        if ($updateResult) {
            return true;
        }
        return false;
//        if ($updateResult) {
//            $url = $_SERVER['HTTP_REFERER'];
//            header("location:$url");
//        }
    }

    protected function processEditNotes($index) {
        $url = $this->model->processEditNotesRequest($index);
        if ($url) {
            echo header($url);
        } else {
            echo 'error';
        }
    }

    protected function processNotesMarkAsComplete($index) {
        $url = $this->model->processNotesMarkAsCompleteRequest($index);
        if ($url) {
            echo header($url);
        } else {
            echo 'error';
        }
    }

}

?>
